export default {
  name: "base64",
  methods: {
    getBase64(imgUrl, name) {
      window.URL = window.URL || window.webkitURL;
      var xhr = new XMLHttpRequest();
      xhr.open("get", imgUrl, true);
      xhr.responseType = "blob";
      xhr.onload = function() {
        if (this.status == 200) {
          //得到一个blob对象
          var blob = this.response;
          // 至关重要
          let oFileReader = new FileReader();
          oFileReader.onloadend = function(e) {
            let base64 = e.target.result;
            localStorage.setItem(name, base64);
          };
          oFileReader.readAsDataURL(blob);
        }
      };
      xhr.send();
    },
    image(name, exception) {
      if (localStorage.getItem(name)) {
        return localStorage.getItem(name);
      } else {
        return exception;
      }
    }
  }
};
